package com.myown.jdk.proxy;

import com.myown.algorithm.GeneticAlgorithm;
import com.myown.structure.Queue;
import com.myown.structure.impl.LinkedQueue;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理 <p>
 *
 * @author lincky
 * @version v1.0.0
 * @time 2017.03.20 16:04
 */
public class DynamicFoodFactory implements InvocationHandler {

    private Object originalObject;

    public Object newProxy(Object object) {
        this.originalObject = object;
        return Proxy.newProxyInstance(object.getClass().getClassLoader(),
                object.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("调用前");
        System.out.println(proxy.getClass() + ";" + originalObject.getClass());
        method.invoke(originalObject, args);
        System.out.println("调用后");
        return null;
    }

    public static void main(String[] args) {
        FoodImpl food = new FoodImpl();
        DynamicFoodFactory foodFactory = new DynamicFoodFactory();
        FoodInterface foodInterface = (FoodInterface) foodFactory.newProxy(food);
        foodInterface.makeHMJ("黄焖鸡");
    }

}
